package com.example.demo.config;

import io.moquette.interception.InterceptHandler;
import io.moquette.interception.messages.*;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
public class MqttMessageHandler  implements InterceptHandler {

    private static final Logger logger = LoggerFactory.getLogger(MqttMessageHandler.class);

    @Override
    public String getID() {
        return "MqttMessageHandler";
    }

    @Override
    public Class<?>[] getInterceptedMessageTypes() {
        return new Class[0];
    }

    @Override
    public void onConnect(InterceptConnectMessage interceptConnectMessage) {

    }

    @Override
    public void onDisconnect(InterceptDisconnectMessage interceptDisconnectMessage) {

    }

    @Override
    public void onConnectionLost(InterceptConnectionLostMessage interceptConnectionLostMessage) {

    }

    @Override
    public void onPublish(InterceptPublishMessage message) {
        ByteBuf payload = message.getPayload();
        String content = payload.toString(StandardCharsets.UTF_8);
        logger.info("Received message on topic {}: {}", message.getTopicName(), content);
    }

    @Override
    public void onSubscribe(InterceptSubscribeMessage interceptSubscribeMessage) {

    }

    @Override
    public void onUnsubscribe(InterceptUnsubscribeMessage interceptUnsubscribeMessage) {

    }

    @Override
    public void onMessageAcknowledged(InterceptAcknowledgedMessage interceptAcknowledgedMessage) {

    }


}

